<?php

$percent_new = 100;
$percent_push = 80;

function getTrkArray ($albStatus = 2, $albPush = 4, $trkTotal = 5, $albCategory = 'all', $albCategoryDescend = true) {
   // {{{we need to put this in the includes instead}}}
   
   $mysqli = new mysqli("localhost", "root", "kaurkaur", "ccramp");
   
   // check connection
   if (mysqli_connect_errno()) {
      printf("Connect failed: %s\n", mysqli_connect_error());
      exit();
   }
   $i = 0; //iterator for building an array of all our albums with status "in rotation"
   $albQuery  = "SELECT `cddbid` FROM `album` WHERE (`status` = '".$albStatus."') AND (`is_comp` = '1') ";
   if ($albPush) $albQuery .= " AND (`virtue` >= '".$albPush."') ";
   if ($trkCollection && ($trkCollection != 'all') ) {
      if ($albCategoryDescend) $albQuery .= " AND ( (`category` = '".$albCategory."') OR (`category3` = '".$albCategory."') OR (`category2` = '".$albCategory."') )";
      else $albQuery .= " AND (`category` = '".$albCategory."') ";
   }
   if ($albResult = $mysqli->query($albQuery)) {
      while ($albTemp = $albResult->fetch_assoc()) {
         $alb[$i++] = $albTemp['cddbid'];
      }
   }
   $newResult = $newAlb = array_rand($alb, $trkTotal);

   $k = 0;
   for ($i = 0; $i < count($newAlb); $i++ ) {
      $j = 0;
      $albId = $alb[$newAlb[$i]];
      $trkQuery = "SELECT `id` FROM `track` WHERE (`cddbid`='".$albId."') AND (`virtue` > '2') AND (`filename` != '' ); ";
      $trkQuery .= "SELECT `id` FROM `track` WHERE (`cddbid`='".$albId."') AND (`virtue` > '3') AND (`filename` != '' ); ";
      $trkQuery .= "SELECT `id` FROM `track` WHERE (`cddbid`='".$albId."') AND (`virtue` > '4') AND (`filename` != '' ) ";
      if ($mysqli->multi_query($trkQuery)) {
         do {
            if ($result = $mysqli->store_result()) {
               while ($row = $result->fetch_row()) {
                  $trk[$j++] = $row[0];
               }
               $result->close();
            }
            $mysqli->more_results();
         } while ($mysqli->next_result());
         $trkArray[$k] = $trk[array_rand($trk, 1)];
         $k++;
         unset($trk);
      }
   }
   // close connection
   $mysqli->close();
   return $trkArray;
}
$trkArray1 = getTrkArray(2, false, 15);
$trkArray2 = getTrkArray(3, false, 1);
$trkArray = array_merge($trkArray1, $trkArray2);

?><pre><?php 

for ($i = 0; $i < count($trkArray); $i++) {
   $trkQuery = mysql_query("SELECT `artist`,`cddbid`,`title` FROM `track` WHERE `id` = '".$trkArray[$i]."' ");
   unset($trk);
   $trk = mysql_fetch_array($trkQuery);
   echo(($i + 1).'. '.$trk[cddbid].'/'.$trk[title].' ('.$trk[artist].')<br />');
}
?></pre><?php
?>
